package com.settlement.system.controller;

import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblRrOtherCapitalDetail;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblRrOtherCapitalDetailService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.util.DateUtils;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Api(tags = "收退款-其他货币资金日记账", description = "收退款-其他货币资金日记账")
@RestController
@RequestMapping("/rr/other_capital/detail/pageBalace")
public class TblRrOtherCapitalDetailBalaceController extends ExcelController<TblRrOtherCapitalDetail> {

    @Value("${file.download.url}")
    private String fileDownloadUrl;

    @Resource
    private ITblRrOtherCapitalDetailService tblRrOtherCapitalDetailService;

    @Override
    public IBaseService init() {
        return tblRrOtherCapitalDetailService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblRrOtherCapitalDetail.class;
    }

    /*@Override
    public String verifyImportObject(TblRrOtherCapitalDetail tblRrOtherCapitalDetail, AnalysisContext analysisContext) {

        if(StringUtils.isEmpty(tblRrOtherCapitalDetail.getNcVirtualAccount())){
            return "NC_虚拟账号不能为空";
        }

        if(StringUtils.isEmpty(tblRrOtherCapitalDetail.getCapitalMonth())){
            return "月份不能为空";
        }

        if(tblRrOtherCapitalDetail.getPageRemainAmount() == null){
            return "页面余额不能为空";
        }

        try {
            DateUtils.parseDate(tblRrOtherCapitalDetail.getCapitalMonth() + "01","yyyyMMdd");
        } catch (Exception ex){
            return "月份格式不正确,格式应该为YYYYMM";
        }

        return null;
    }*/

    @Override
    protected void saveData(List<TblRrOtherCapitalDetail> datas) throws ServiceException {
        tblRrOtherCapitalDetailService.savePageRemainAmount(datas);
    }

    @Override
    protected List<TblRrOtherCapitalDetail> getExportDate(Map<String, Object> params) throws ServiceException {
        List<TblRrOtherCapitalDetail> list = super.getExportDate(params);
        if(!CollectionUtils.isEmpty(list)){
            for(TblRrOtherCapitalDetail info:list){
                if(!StringUtils.isEmpty(info.getFileUrl())){
                    info.setFileUrl(fileDownloadUrl + info.getFileUrl());
                }
            }
        }
        return list;
    }


    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        List<TblRrOtherCapitalDetail> rows = (List<TblRrOtherCapitalDetail>) result.get("rows");
        if(!CollectionUtils.isEmpty(rows)){
            for(TblRrOtherCapitalDetail info:rows){
                if(!StringUtils.isEmpty(info.getFileUrl())){
                    info.setFileUrl(fileDownloadUrl + info.getFileUrl());
                }
            }
        }
    }
}
